package addmi2_test;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import java.util.Random;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class index {

	protected Shell shell;
	private Text text;
	private Text text_1;
	private Text text_2;
	private Label lblNewLabel_2;
	private Random r = new Random();
	private int a[] = new int[10];	
	private Text text_4;
	private Label lblNewLabel_5;
	private Button btnNewButton_1;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			index window = new index();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(658, 513);
		shell.setText("SWT Application");
		
		text = new Text(shell, SWT.BORDER);
		text.setBounds(186, 47, 374, 37);
		
		text_1 = new Text(shell, SWT.BORDER);
		text_1.setBounds(186, 123, 374, 37);
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(72, 50, 40, 20);
		lblNewLabel.setText("明文");
		
		Label lblNewLabel_1 = new Label(shell, SWT.NONE);
		lblNewLabel_1.setBounds(72, 142, 40, 20);
		lblNewLabel_1.setText("暗文");
		
		text_2 = new Text(shell, SWT.BORDER);
		text_2.setBounds(186, 208, 374, 37);
		
		lblNewLabel_2 = new Label(shell, SWT.NONE);
		lblNewLabel_2.setBounds(84, 211, 29, 20);
		lblNewLabel_2.setText("n");
		
		Button btnNewButton = new Button(shell, SWT.NONE);

		btnNewButton.setBounds(281, 396, 98, 30);
		btnNewButton.setText("加密");
		
		text_4 = new Text(shell, SWT.BORDER);
		text_4.setBounds(186, 296, 374, 37);
		
		Label lblNewLabel_4 = new Label(shell, SWT.NONE);
		lblNewLabel_4.setBounds(82, 310, 76, 20);
		lblNewLabel_4.setText("d");
		
		lblNewLabel_5 = new Label(shell, SWT.NONE);
		lblNewLabel_5.setForeground(SWTResourceManager.getColor(255, 0, 0));
		lblNewLabel_5.setBounds(36, 412, 108, 20);
		
		btnNewButton_1 = new Button(shell, SWT.NONE);

		btnNewButton_1.setBounds(462, 396, 98, 30);
		btnNewButton_1.setText("解密");

		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int p = r.nextInt(98)+2;
				int q = r.nextInt(98)+2;
				while(!judge(p)||!judge(q)) {
					if(!judge(p)) {
						p = r.nextInt(98)+2;
					}
					if(!judge(q)) {
						q = r.nextInt(98)+2;
					}
				}
				System.out.println("p=" + p + ",q=" + q);
				int n = p*q;
				text_2.setText(n+"");
				int Eint = createE(p,q);
				String addstr = text.getText();
				if(addstr == null || "".equals(addstr)) {
					lblNewLabel_5.setText("没有输入明文");
				}
				int addmis = Integer.parseInt(addstr);
				text_1.setText(addmi(addmis, n, Eint)+"");
				int $n = (p-1)*(q-1);	
				int Dint = getD($n,Eint);
				text_4.setText(Dint+"");
			}

			private int createE(int p, int q) {
				int $n = (p-1)*(q-1);		
				int b = 0;
				int c = 0;//a数组的索引
				for(int i=2;i<$n;i++) {
					b = $n/i;
					if(b*i == $n) {
						if(c >= a.length) {
							add(a.length);
						}
						a[c++] = i;
					}
				}
				c=0;
				for(int i=2;i<$n; i++) {
						if(!judge2(i)){
							continue;
						}
						if(judge(i)) {
							return i;
						}
						for(int k = 2;k<i;k++) {
							c = i/k;
							if(c*k == i) {
								if(judge2(c)&&judge2(k)) {
									return i;
								}
							}
						}
					
				}
				return 0;
			}

			private boolean judge2(int c) {
				for(int i=0;i<a.length;i++) {
					if(a[i] == c) {
						return false;
					}
				}
				return true;
			}

			private void add(int length) {
				int as [] = new int[length*2];
				System.arraycopy(a, 0, as, 0, length);
				a = as;
			}
		});
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				index3 window = new index3();
				window.open();
			}
		});
	}
	
	private static boolean judge(int k) {
		int s = 0;
		for(int a = 2;a<k;a++) {
			s = k/ a;
			if(s * a == k) {
				return false;
			}
		}
		return true;
	}
	
	//加密
	private static int addmi(int count, int n, int e) {
		int c =1;
		for(int i=1;i<=e;i++) {
			c = c*count;
			if(c>n) {
				c = c%n;
			}
		}
		return c;
	}
	
	//获得d
	private static int getD(int $n, int e) {
		int H = 0;
		int d = 0;
		for(int i = 1;true; i++) {
			H = e*i;
			if(H%$n == 1) {
				return i;
			}
			if(i>=1000000000) {
				return 0;
			}
		}
	}
}
